package com.csu.letaobackend.controller;

import com.csu.letaobackend.bean.Receiving;
import com.csu.letaobackend.bean.Seller;
import com.csu.letaobackend.bean.User;
import com.csu.letaobackend.commom.CommonResponse;
import com.csu.letaobackend.service.OrderService;
import com.csu.letaobackend.vo.OrderItemVO;
import com.csu.letaobackend.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@CrossOrigin
@RequestMapping("/orders")
public class OrderController {

      @Autowired
      OrderService orderService;

      @GetMapping("")
      @ResponseBody
      public CommonResponse<List<OrderVO>> getOrders(@RequestParam int status, HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            return orderService.getOrders(login_user.getUsername(), status);
      }


      @GetMapping("/{orderid}")
      @ResponseBody
      public CommonResponse<OrderVO> getOrderItems(@PathVariable String orderid, HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            return orderService.getOrderByOrderId(orderid);
      }

      @PostMapping("/{orderid}/cancel")
      @ResponseBody
      public CommonResponse cancelOrder(@PathVariable String orderid, HttpSession httpSession) {
            if (orderService.updateOrderStatus(orderid, -1).getStatus() == 0)
                  return CommonResponse.createForSuccessMessage("取消订单成功");
            else
                  return CommonResponse.createForError("取消失败，不存在该订单");
      }


      @PostMapping("/{orderid}/pay")
      @ResponseBody
      public CommonResponse payOrder(@PathVariable String orderid, HttpSession httpSession) {
            if (orderService.updateOrderStatus(orderid, 1).getStatus() == 0)
                  return CommonResponse.createForSuccessMessage("付款成功");
            else
                  return CommonResponse.createForError("付款失败");
      }

      @PostMapping("/{orderid}/deliver")
      @ResponseBody
      public CommonResponse deliverOrder(@PathVariable String orderid, HttpSession httpSession) {
            if (orderService.updateOrderStatus(orderid, 2).getStatus() == 0)
                  return CommonResponse.createForSuccessMessage("发货成功");
            else
                  return CommonResponse.createForError("发货失败");
      }

      @PostMapping("/{orderid}/receipt")
      @ResponseBody
      public CommonResponse receiptOrder(@PathVariable String orderid, HttpSession httpSession) {
            if (orderService.updateOrderStatus(orderid, 3).getStatus() == 0)
                  return CommonResponse.createForSuccessMessage("确认收货成功");
            else
                  return CommonResponse.createForError("确认收货失败");
      }

      @PostMapping("/{orderid}/return")
      @ResponseBody
      public CommonResponse returnOrder(@PathVariable String orderid, @RequestParam String refundreason, HttpSession httpSession) {
            orderService.updateOrderStatus(orderid, 4).getStatus();
            orderService.addRefundservice(orderid, refundreason);
            return CommonResponse.createForSuccessMessage("已申请退货");
      }

      @PostMapping("/{orderid}/confirmreturn")
      @ResponseBody
      public CommonResponse confirmreturnOrder(@PathVariable String orderid) {
            orderService.updateOrderStatus(orderid, 5);
            orderService.refuseRefundservice(orderid, "同意退货");
            return CommonResponse.createForSuccessMessage("同意退货成功");
      }

      @GetMapping("/refund/{orderid}")
      @ResponseBody
      public CommonResponse getRefundservice(@PathVariable String orderid) {

            return orderService.getRefundservice(orderid);
      }

      @PostMapping("/{orderid}/refusereturn")
      @ResponseBody
      public CommonResponse refusereturnOrder(@PathVariable String orderid, @RequestParam String refusalreason) {
            orderService.updateOrderStatus(orderid, 6);
            orderService.refuseRefundservice(orderid, refusalreason);
            return CommonResponse.createForSuccessMessage("拒绝退货成功");
      }


      @PostMapping("/{orderID}/alipay")
      @ResponseBody
      public CommonResponse<String> AlipayOrder(HttpSession session, @PathVariable("orderID") String orderID, @RequestParam("total") double total) throws Exception {
            User login_user = (User) session.getAttribute("login_user");
            if (null == login_user) {
                  return CommonResponse.createForError("用户未登录");
            }
            return orderService.handleAlipay(orderID, String.format("%.2f", total), login_user.getUsername());
      }

      @PostMapping("/notify_url")
      public void Notify(String orderID) {
            orderService.orderNotify(orderID);
      }


      @GetMapping("/receivings")
      @ResponseBody
      public CommonResponse<List<Receiving>> getReceivings(HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            return orderService.getReceivingByUsername(login_user.getUsername());
      }

      @GetMapping("/receivings/{receivingid}")
      @ResponseBody
      public CommonResponse<Receiving> getReceivingById(@PathVariable int receivingid, HttpSession httpSession) {

            return orderService.getReceivingById(receivingid);
      }

      @PostMapping("/receivings/{receivingid}")
      @ResponseBody
      public CommonResponse<List<Receiving>> addOrUpdateReceiving(@PathVariable int receivingid, Receiving receiving, HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            receiving.setUsername(login_user.getUsername());
            return orderService.updateReceiving(receiving);
      }

      @GetMapping("/seller")
      @ResponseBody
      CommonResponse<List<OrderVO>> getOrdersBySellers(@RequestParam int status, HttpSession httpSession) {
            Seller login_seller = (Seller) httpSession.getAttribute("login_seller");
            return orderService.getOrdersBySellers(login_seller.getUsername(), status);
      }

      @GetMapping("/shops/{shopid}")
      @ResponseBody
      CommonResponse<List<OrderVO>> getOrdersByShopId(@PathVariable int shopid, @RequestParam int status, HttpSession httpSession) {
            return orderService.getOrdersByShopId(shopid, status);
      }


}
